package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;

import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.BaseFragment;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.migu.contacts.R;
import com.migu.contacts.bean.CalendarBean;
import com.migu.contacts.bean.EventBean;
import com.migu.contacts.bean.MonthBean;
import com.migu.contacts.databinding.FramgentCalendarBinding;
import com.migu.contacts.databinding.ItemCalendar1Binding;
import com.migu.contacts.databinding.ItemCalendar2Binding;
import com.migu.contacts.databinding.ItemCalendarBinding;
import com.migu.contacts.databinding.ItemCalendarEventBinding;
import com.migu.contacts.main.todo.CreateTodoActivity;

import org.greenrobot.eventbus.EventBus;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CalendarFragment extends BaseFragment<FramgentCalendarBinding> {
    private Calendar getSchemeCalendar(int year, int month, int day, int color) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        return calendar;
    }

    private Calendar getSchemeCalendar(String date) {
        Calendar calendar = new Calendar();
        LocalDate parse = LocalDate.parse(date);
        calendar.setYear(parse.getYear());
        calendar.setMonth(parse.getMonth().getValue());
        calendar.setDay(parse.getDayOfMonth());
        calendar.setSchemeColor(Color.RED);//如果单独标记颜色、则会使用这个颜色
        return calendar;
    }


    @Override
    protected void initView() {
        super.initView();
        CalendarView calendarView = mBinding.calendarView;
        int curYear = calendarView.getCurYear();
        int curMonth = calendarView.getCurMonth();
        mBinding.tvYear.setText(curYear + "." + curMonth);
        calendarView.setOnMonthChangeListener((year, month) -> {
            Log.d(TAG, "initView() called" + year + ":" + month);
            mBinding.tvYear.setText(year + "." + month);
            currentMonth = String.format("%d-%02d", curYear, curMonth);
            getMothData();
        });

        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                int day = calendar.getDay();
                int year = calendar.getYear();
                int month = calendar.getMonth();
                Log.i(TAG, "onCalendarSelect: " + day + ":" + year + ":" + month);
                currentDate = LocalDate.of(year, month, day).toString();
                getData();
            }
        });
        int day = calendarView.getCurDay();
        currentDate = LocalDate.of(curYear, curMonth, day).toString();
        currentMonth = String.format("%d-%02d", curYear, curMonth);
    }

    String currentDate = null;
    String currentMonth = null;

    @Override
    public void onResume() {
        super.onResume();
        getMothData();
        getData();
    }

    private void getMothData() {
        API.getCalendarMonth(currentMonth).watch(this, new HttpCallback<List<MonthBean>>() {
            @Override
            protected void onSuccess(List<MonthBean> eventBeans) {
                Log.d(TAG, "onSuccess() called with: eventBeans = [" + eventBeans + "]");
                Map<String, Calendar> map = new HashMap<>();
                for (MonthBean eventBean : eventBeans) {
                    Calendar schemeCalendar = getSchemeCalendar(eventBean.date);
                    map.put(schemeCalendar.toString(), schemeCalendar);
                }
                mBinding.calendarView.setSchemeDate(map);
            }
        });
    }

    private void getData() {
        API.getCalendarDay(currentDate).watch(mActivity, new HttpCallback<List<EventBean>>() {
            @Override
            protected void onSuccess(List<EventBean> birthdayWordsBeans) {
                Log.d(TAG, "onSuccess() called with: birthdayWordsBeans = [" + birthdayWordsBeans + "]");
                for (CalendarBean calendarBean : list) {
                    calendarBean.list.clear();
                }
                list1.clear();
                list2.clear();
                for (EventBean bean : birthdayWordsBeans) {
                    if (bean.timeType == 1) {
                        list1.add(bean);
                    } else if (bean.timeType == 2) {
                        list2.add(bean);
                    } else {
                        int hour = bean.hour;
                        list.get(hour).list.add(bean);
                    }
                }
                adapter1.setData(list1);
                adapter2.setData(list2);
                adapter.notifyDataSetChanged();
            }
        });
    }

    List<CalendarBean> list = new ArrayList<>();
    List<EventBean> list1 = new ArrayList<>();
    List<EventBean> list2 = new ArrayList<>();

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void initData() {
        super.initData();
        for (int i = 0; i < 24; i++) {
            String format = String.format("%02d:00", i);
            list.add(new CalendarBean(format));
        }
        adapter.setData(list);
    }

    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemCalendarBinding, CalendarBean> adapter = new BaseAdapter<ItemCalendarBinding, CalendarBean>() {
        @OnClick(R.id.add)
        public void add(CalendarBean bean) {
            Log.d(TAG, "add() called with: bean = [" + bean.time + "]");
            start(CreateTodoActivity.class);
        }

        @Override
        protected void onBindData(ItemCalendarBinding binding, CalendarBean bean, int position) {
            super.onBindData(binding, bean, position);
            BaseAdapter<ItemCalendarEventBinding, EventBean> childAdapter = new BaseAdapter<ItemCalendarEventBinding, EventBean>() {
                @OnClick(R.id.root)
                public void go(EventBean eventBean) {
                    start(eventBean);
                }
            };
            binding.recyclerView.setAdapter(childAdapter);
            childAdapter.setData(bean.list);
            if (bean.list.size() > 0) {
                binding.add.setVisibility(View.GONE);
            } else {
                binding.add.setVisibility(View.VISIBLE);
            }
        }
    };
    @BindAdapter(R.id.recyclerView1)
    BaseAdapter<ItemCalendar1Binding, EventBean> adapter1 = new BaseAdapter<ItemCalendar1Binding, EventBean>() {
        @OnClick(R.id.root)
        public void go(EventBean eventBean) {
            start(eventBean);
        }
    };

    private void start(EventBean eventBean) {
        Log.d(TAG, "start() called with: eventBean = [" + eventBean + "]");
//        类型 1=生日 2=节日 3=礼物 4=普通 5=周期
        switch (eventBean.type) {
//            case 1:
//                //生日
//                break;
//            case 2:
//                //生日
//                break;
//            case 3:
//                //生日
//                break;
            case 4:  //普通
                EventBus.getDefault().postSticky(eventBean);
                start(CreateTodoActivity.class);
                break;
//            case 5:
//                //生日
//                break;
            default:
                Log.i(TAG, "start: " + eventBean.type);
        }
    }

    @BindAdapter(R.id.recyclerView2)
    BaseAdapter<ItemCalendar2Binding, EventBean> adapter2 = new BaseAdapter<ItemCalendar2Binding, EventBean>() {
        @OnClick(R.id.root)
        public void go(EventBean eventBean) {
            start(eventBean);
        }
    };
}
